package com.zl.easybuy.service.impl;

import com.zl.easybuy.bo.ProductBo;
import com.zl.easybuy.mapper.ProductMapper;
import com.zl.easybuy.pojo.Product;
import com.zl.easybuy.service.ProductService;
import com.zl.easybuy.util.Pages;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Override
    public Product findProductById(Integer id) {
        if(id==null){
            return null;
        }
        return productMapper.findById(id);
    }

    @Override
    public Integer count(ProductBo productBo) {
        return productMapper.countBy(productBo);
    }

    @Override
    public List<Product> findProductListBy(ProductBo productBo) {
        return productMapper.findProducts(productBo);
    }

    @Override
    public Pages<Product> findProductList(Integer category,Integer level, String keyWord,Integer pageNo) {
        //搜索条件体
        ProductBo productBo=new ProductBo();
        productBo.setPage(true);
        productBo.setPageSize(6);
        productBo.setPageNo(pageNo);
        Product product=new Product();
        if(keyWord!=null&&keyWord!=""){
            //根据名字模糊查询
            product.setName(keyWord);
        }
        if(level!=null){
            switch (level) {
                case 1:
                    product.setCategoryLevel1Id(category);
                    break;
                case 2:
                    product.setCategoryLevel2Id(category);
                    break;
                case 3:
                    product.setCategoryLevel3Id(category);
                    break;
                default:
                    break;
            }
        }
        productBo.setProduct(product);
        //前端页面数据
        Pages<Product> pages=new Pages<>();
        pages.setUrl(createUrl(category, level, keyWord, pageNo));
        pages.setPageNo(pageNo);
        //页大小
        pages.setPageSize(5);
        pages.setTotalCount(productMapper.countBy(productBo));
        pages.setRecord(productMapper.findProducts(productBo));
        return pages;
    }

    //url拼接
    private String createUrl(Integer category, Integer level, String keyword, Integer pageNo) {
        StringBuffer url = new StringBuffer("/Product/queryProductList?time=");
        url.append(System.currentTimeMillis());
        if(category!=null){
            url.append("&category="+category);
        }
        if(level!=null){
            url.append("&level="+level);
        }
        if(keyword!=null&&keyword!=""){
            url.append("&keyword="+keyword);
        }
        return url.toString();
    }

}
